# -*- coding: utf-8 -*-
import smtplib
import email.MIMEMultipart
import email.MIMEText
import email.MIMEBase
import os.path
from email.header import Header
from email.mime.text import MIMEText

# from utils.tel_num import TEL_NUM
TEL_NUM = "17791269154"
# MAIL_HOST = "smtp.163.com"
# MAIL_USER = "gooliangcloud@163.com"
# MAIL_PASS = "guliang1717"
# MAIL_POSTFIX = "163.com"
# MAIL_HOST = "smtp.163.com"
# MAIL_USER = "17629202572@163.com"
# MAIL_PASS = "960722zx"
# MAIL_PASS = "wangqian123"
# MAIL_POSTFIX = "163.com"


MAIL_HOST = "smtp.163.com"
MAIL_USER = "dintonhr@163.com"
MAIL_PASS = "dinton123"
MAIL_POSTFIX = "163.com"


def send():
    From = ("%s<" + MAIL_USER + "@" + MAIL_POSTFIX + ">") % (Header("鼎拓科技", 'utf-8'),)
    # From = "dintonhr@163.com"
    To = "zhangxin183913@163.com"
    file_name = "C:/Users/dt/Desktop/91101.jpg"
    
    server = smtplib.SMTP(MAIL_HOST)
    server.login(MAIL_USER, MAIL_PASS)  # 仅smtp服务器需要验证时
    
    # 构造MIMEMultipart对象做为根容器
    main_msg = email.MIMEMultipart.MIMEMultipart()
    # body = "您好，你有快递到达"
    # main_msg = MIMEText(body, format, 'utf-8')
    # 构造MIMEText对象做为邮件显示内容并附加到根容器
    text_msg = email.MIMEText.MIMEText("this is a test text to text mime")
    main_msg.attach(text_msg)
    
    # 构造MIMEBase对象做为文件附件内容并附加到根容器
    contype = 'application/octet-stream'
    maintype, subtype = contype.split('/', 1)
    
    ## 读入文件内容并格式化
    data = open(file_name, 'rb')
    file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
    file_msg.set_payload(data.read())
    data.close()
    email.Encoders.encode_base64(file_msg)
    
    ## 设置附件头
    basename = os.path.basename(file_name)
    file_msg.add_header('Content-Disposition',
                        'attachment', filename=basename)
    main_msg.attach(file_msg)
    
    # 设置根容器属性
    main_msg['From'] = From
    main_msg['To'] = To
    main_msg['Subject'] = "attach test "
    main_msg['Date'] = email.Utils.formatdate()
    
    # 得到格式化后的完整文本
    fullText = main_msg.as_string()
    
    # 用smtp发送邮件
    try:
        server.sendmail(From, To, fullText)
    finally:
        server.quit()


# def email_public(sign, content_param):
#     """邮件发送接口"""
#     if sign == 0:
#         if not content_param['user_email']:
#             print("user_email is null..")
#             return False
#         else:
#             content = "{user_name}您好，西安鼎拓信息科技有限公司诚邀您{date}" \
#                       "携带个人简历来公司参加面试。地址：西安高新区丈八一路绿地soho同盟b座710" \
#                       "室，有问题可拨{tel_num}联系。收到请回复，谢谢！"
#
#             content_info = content.format(user_name=content_param['user_name'], date=content_param['date'],
#                                           tel_num=TEL_NUM)
#             if send_mail(content_param['user_email'], "interview", content_info, format='plain'):
#                 return True
#             else:
#                 return False


if __name__ == '__main__':
    send()
